package com.rxt.thermopro.ui.dialog

import android.app.Dialog
import android.graphics.drawable.ColorDrawable
import android.media.MediaPlayer
import android.os.Bundle
import android.util.DisplayMetrics
import android.util.Log
import android.view.Gravity
import android.view.View
import android.view.Window
import android.view.WindowManager
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import com.app.widget.PressTextView
import com.app.widget.loopview.LoopView
import com.app.widget.loopview.OnItemScrollListener
import com.rxt.thermopro.R
import com.rxt.thermopro.app.AppContext
import com.rxt.thermopro.app.LocalDataDao
import java.util.*

class ProbeAlarmBottomDialog : DialogFragment() {
    private lateinit var contentView: View
    private var isShow = false
    private var mScreenDensity = 0f
    private var mScreenHeight = 0
    private var mScreenWidth = 0
    private lateinit var viewCancel: PressTextView
    private lateinit var viewOk: PressTextView
    private lateinit var loopView: LoopView
    private var mListener: OnItemSelectListener? = null

    interface OnItemSelectListener {
        fun onSelected(index: Int, indexString: String)
    }

    fun setOnItemSelectListener(listener: OnItemSelectListener) {
        mListener = listener
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = Dialog(requireActivity())

        contentView = View.inflate(requireContext(), R.layout.dialog_probe_alarm_bottom, null)
        initListener()

        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
        dialog.setContentView(contentView)
        dialog.setCanceledOnTouchOutside(false)

        val displayMetrics = DisplayMetrics()
        requireActivity().windowManager.defaultDisplay.getMetrics(displayMetrics)
        mScreenDensity = displayMetrics.density
        mScreenHeight = displayMetrics.heightPixels
        mScreenWidth = displayMetrics.widthPixels

        isCancelable = true
        val window: Window? = dialog.window
        if (window != null) {
            window.setBackgroundDrawable(ColorDrawable(resources.getColor(android.R.color.transparent)))
            window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
            val lp: WindowManager.LayoutParams = window.attributes
            lp.gravity = Gravity.BOTTOM
            lp.width = mScreenWidth
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT
            window.attributes = lp
        }
        return dialog
    }

    private var currentIndex = 0
    private var mMediaPlayer: MediaPlayer? = null
    private fun initListener() {
        viewCancel = contentView.findViewById(R.id.viewCancel)
        viewOk = contentView.findViewById(R.id.viewOk)
        loopView = contentView.findViewById(R.id.loopView)

        val list = LocalDataDao.INSTANCE.getProbeAlarmList()

        viewCancel.setOnClickListener {
            hideDialog()
        }

        viewOk.setOnClickListener {
            hideDialog()
            mListener?.onSelected(currentIndex, list[currentIndex].ringName)
        }


        //滚动监听
        loopView.setListener { index ->
            currentIndex = index
            if (mMediaPlayer?.isPlaying == true) {
                mMediaPlayer?.stop()
            }
            if (index != 0) {
                mMediaPlayer = MediaPlayer.create(
                    AppContext.get(),
                    list[currentIndex].ringUri
                )
                mMediaPlayer?.isLooping = true
                mMediaPlayer?.start()
            }
        }
        loopView.setOnItemScrollListener(object : OnItemScrollListener {
            override fun onItemScrollStateChanged(
                loopView: LoopView?,
                currentPassItem: Int,
                oldScrollState: Int,
                scrollState: Int,
                totalScrollY: Int
            ) {

            }

            override fun onItemScrolling(
                loopView: LoopView?,
                currentPassItem: Int,
                scrollState: Int,
                totalScrollY: Int
            ) {

            }
        })
        //设置原始数据
        loopView.setItems(list.map { it.ringName })
        //设置初始位置
        loopView.setInitPosition(currentIndex)
    }

    fun setSelectPosition(index: Int) {
        currentIndex = index
    }

    fun showDialog(fragmentManager: FragmentManager) {
        try {
            if (!isShow) {
                this.show(fragmentManager, "TAG")
                isShow = true
            }
        } catch (e: Exception) {
            e.printStackTrace()
            Log.d("123->", "showDialog Exception $e")
        }
    }

    fun hideDialog() {
        if (isShow) {
            dismiss()
            isShow = false
        }
    }

    override fun dismiss() {
        super.dismiss()
        if (mMediaPlayer?.isPlaying == true) {
            mMediaPlayer?.stop()
        }
    }

    override fun onResume() {
        super.onResume()
        isShow = true
    }

    override fun onPause() {
        super.onPause()
        isShow = false
    }

    override fun show(manager: FragmentManager, tag: String?) {
        val ft = manager.beginTransaction()
        ft.add(this, tag)
        ft.commitAllowingStateLoss()
    }
}